package vn.vfriends.soffice.facade;

import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import vn.vfriends.soffice.entity.Menu;


/**
 *
 * @author chaungoctuan@gmail.com
 */
@Stateless
@LocalBean
public class MenuFacade extends AbstractFacade<Menu> {

    @PersistenceContext(unitName = "sOfficePU")
    private EntityManager em;

    public MenuFacade() {
        super(Menu.class);
    }
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
    public Menu findByName(String name){
        Query query = em.createNamedQuery("Menu.findByName", Menu.class);
        query.setParameter("name", name);
        List<Menu> menuList = query.getResultList();
        if(menuList != null && !menuList.isEmpty())
            return menuList.get(0);
        else
            return null;
    }

    public List<Menu> findChildren(Menu parent) {
        Query query = em.createNamedQuery("Menu.findChildren", Menu.class);
        query.setParameter("parent", parent);
        return query.getResultList();
    }

    public List<Menu> findFirstLevel() {
        Query query = em.createNamedQuery("Menu.findFirstLevel", Menu.class);
        return query.getResultList();
    }
}
